home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / N-P / NIFTY / myCShell / meter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-17  |  4.0 KB  |  185 lines  |  [TEXT/KAHL]

  1. /************************************************************
  2.  "meter.c"
  3.  
  4.  by:                    Walt Davis [MacTutor, September 1991]
  5.  
  6.  converted to "C" by:    John A. Love, III
  7.                          using Symantec's "THINK C", v 5.00
  8.  ************************************************************/
  9.  
  10.  
  11. #include <Palettes.h>
  12.  
  13. #include "protos"
  14.  
  15. #include "globals.h"
  16. #include "extern.h"
  17.  
  18.  
  19.  
  20.  
  21. WindowPtr    MeterWindowInit (void)    {
  22.  
  23.         Rect        meterWindRect = {0, 0, 60, 280};
  24.         WindowPtr    meterWindow;
  25.         
  26.     
  27.     meterWindow = NewWindow(nil,
  28.                             &meterWindRect,
  29.                             "",
  30.                             false,
  31.                             dBoxProc,
  32.                             (WindowPtr)-1,
  33.                             false,
  34.                             0L);
  35.     SetPort(meterWindow);
  36.     
  37.     CenterWindow(meterWindow);
  38.     ShowWindow(meterWindow);
  39.     
  40.     return (meterWindow);
  41.  
  42. }    /* MeterWindowInit */
  43.  
  44.  
  45. void    MeterWindowDraw (WindowPtr meterWindow, Str255 mTitle)    {
  46.  
  47.         /* Where the 5% and 10% graduation marks are
  48.         ** placed given a rect 250 pixels wide:         */
  49.         short        fiveSize = 2, fiveStep = 12;
  50.         short        tenSize  = 4, tenStep  = 25;
  51.         
  52.         GrafPtr        savePort;
  53.         PenState    pnState;
  54.         RGBColor    foreColor, backColor;
  55.                     // Centered within window {faster than calling _InsetRect}:
  56.         Rect        meterRect = {22, 15, 22+16, 15+250};
  57.         short        grad, pnSize = 1;
  58.         
  59.         
  60.     GetPort(&savePort);
  61.     SetPort(meterWindow);
  62.     
  63.     EraseRect(&meterWindow->portRect);
  64.     
  65.     GetPenState(&pnState);
  66.     if (gMac2)        {
  67.         GetForeColor(&foreColor);
  68.         GetBackColor(&backColor);
  69.         PmForeColor(redColor);
  70.         PmBackColor(yellowColor);
  71.     }
  72.  
  73.     PenNormal();
  74.     TextFont(systemFont);
  75.     TextSize(12);
  76.     PenSize(pnSize, pnSize);
  77.     
  78.     EraseRect(&meterRect);
  79.     InsetRect(&meterRect, -pnSize, -pnSize);
  80.     FrameRect(&meterRect);
  81.     InsetRect(&meterRect, pnSize, pnSize);
  82.     
  83.     // Draw the first 5% graduation mark:
  84.     
  85.     MoveTo(meterRect.left + fiveStep, meterRect.top);
  86.     Line(0, fiveSize);
  87.     MoveTo(meterRect.left + fiveStep, meterRect.bottom);
  88.     Line(0, -fiveSize);
  89.     
  90.     // Draw the remaining graduation Marks:
  91.     
  92.     for (grad = 1; grad <= 9; grad++)    {
  93.         MoveTo(meterRect.left + grad*tenStep - pnSize, meterRect.top);
  94.         Line(0, tenSize);
  95.         MoveTo(meterRect.left + grad*tenStep - pnSize, meterRect.bottom);
  96.         Line(0, -tenSize);
  97.         
  98.         MoveTo(meterRect.left + grad*tenStep + fiveStep, meterRect.top);
  99.         Line(0, fiveSize);
  100.         MoveTo(meterRect.left + grad*tenStep + fiveStep, meterRect.bottom);
  101.         Line(0, -fiveSize);
  102.     }
  103.     
  104.     // Draw the "horizontal axis" of the dynamic rect:
  105.     
  106.     MoveTo(meterRect.left - 5, meterRect.bottom + 16);
  107.     DrawString("\p0%");
  108.     MoveTo(meterRect.left + 65, meterRect.bottom + 16);
  109.     DrawString("\pPercent Complete");
  110.     MoveTo(meterRect.right - 20, meterRect.bottom + 16);
  111.     DrawString("\p100%");
  112.         
  113.     // Draw the window's "title":
  114.     
  115.     MoveTo(meterRect.left, meterRect.top - 5);
  116.     DrawString(mTitle);
  117.     
  118.     if (gMac2)        {
  119.         RGBForeColor(&foreColor);
  120.         RGBBackColor(&backColor);
  121.     }
  122.     SetPenState(&pnState);
  123.     SetPort(savePort);
  124.  
  125. }    /* MeterWindowDraw */
  126.  
  127.  
  128. void    MeterWindowUpdate (WindowPtr meterWindow, Str255 mTitle,
  129.                            short currStep, short maxSteps)    {
  130.  
  131.  
  132.     MeterWindowDraw(meterWindow, mTitle);
  133.     MeterUpdate(meterWindow, currStep, maxSteps);
  134.  
  135. }    /* MeterWindowUpdate */
  136.  
  137.  
  138. void    MeterUpdate (WindowPtr meterWindow, short currStep, short maxSteps)    {
  139.  
  140.         GrafPtr        savePort;
  141.         Rect        meterRect = {22, 15, 22+16, 15+250};
  142.         RGBColor    foreColor;
  143.         short        pDone;
  144.  
  145.  
  146.     GetPort(&savePort);                    // Could be in back, you all !!!
  147.     if (gMac2)        {
  148.         GetForeColor(&foreColor);
  149.         PmForeColor(greenColor);
  150.     }
  151.     SetPort(meterWindow);
  152.     
  153.     if (maxSteps == 0)        pDone = 0;
  154.     else    {
  155.         /* A kludgy way to round-up ... BUT it works !!
  156.         ** ... it sure beats using the full ANSI library
  157.         ** which significantly increases the Project size: */
  158.         
  159.         pDone = (currStep * 10000) / maxSteps;
  160.         if (pDone > ( ((currStep*100)/maxSteps) * 100 ))    pDone = pDone/100 + 1;
  161.         else                                                pDone = pDone/100;
  162.         if (pDone > 100)    pDone = 100;
  163.     }
  164.     
  165.     meterRect.right = meterRect.left + pDone*2.5;
  166.     FillRect(&meterRect, ltGray);
  167.     
  168.     if (gMac2)        RGBForeColor(&foreColor);
  169.     SetPort(&savePort);
  170.  
  171. }    /* MeterUpdate */
  172.  
  173.  
  174. void    MeterWindowKill (WindowPtr meterWindow)    {
  175.  
  176.  
  177.     DisposeWindow(meterWindow);
  178.  
  179. }    /* MeterWindowKill */
  180.  
  181.  
  182.  
  183.  
  184. /*    { end file "meter.c" }  */
  185.